package com.yc.rh.actions;

import java.util.Map;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;
import com.yc.rh.times.MyTimer;
import com.yc.rh.utils.MailUtil;
import com.yc.rh.utils.SessionAttributeNames;
import com.yc.rh.utils.ReflectUtils;
import com.yc.rh.biz.IUserInfoBiz;
import com.yc.rh.entities.JsonObject;
import com.yc.rh.entities.PageInfo;
import com.yc.rh.entities.UserInfo;

public class UserInfoAction extends ActionSupport implements SessionAware,ServletRequestAware{
	private static final long serialVersionUID = -5218732240029095192L;
	private IUserInfoBiz userInfoBiz;
	private JsonObject<UserInfo> jsonObject;
	private UserInfo userInfo;
	private Map<String,Object> session;
	private HttpServletRequest request;
	private PageInfo pageInfo;

	public IUserInfoBiz getUserInfoBiz() {
		return userInfoBiz;
	}

	public void setUserInfoBiz(IUserInfoBiz userInfoBiz) {
		this.userInfoBiz = userInfoBiz;
	}

	public JsonObject<UserInfo> getJsonObject() {
		return jsonObject;
	}

	public void setJsonObject(JsonObject<UserInfo> jsonObject) {
		this.jsonObject = jsonObject;
	}

	public UserInfo getUserInfo() {
		return userInfo;
	}

	public void setUserInfo(UserInfo userInfo) {
		this.userInfo = userInfo;
	}

	public void setSession(Map<String, Object> session) {
		this.session = session;
	}
	
	public void setServletRequest(HttpServletRequest arg0) {
		this.request=arg0;
	}

	public PageInfo getPageInfo() {
		return pageInfo;
	}

	public void setPageInfo(PageInfo pageInfo) {
		this.pageInfo = pageInfo;
	}

	/**
	 * 发送邮箱
	 * @return
	 */
	public String sendEmail(){
		String email=userInfo.getEmail();
		System.out.println("在这里"+email);
		//随机生成6位的验证码
		Random rd=new Random();
		String str="";
		while(str.length()<6){
			str+=rd.nextInt(10);
		}
		System.out.println("验证码    "+str);

		//将验证码存入session
		//session.setAttribute(SessionAttributeNames.CHECKCODE, str);
		session.put(SessionAttributeNames.CHECKCODE, str);
		//启用定时任务，2分钟后清空session重点验证码

		//将验证码发送到用户邮箱
		MailUtil mailUtil=new MailUtil();
		if(mailUtil.sendMail(email, "韩国艺匠婚纱网注册验证码", str)){
//						MyTimer myTimer=new MyTimer();
//						myTimer.getTimeer(session);
			jsonObject.setResult(1);;  //发送成功
		}else{
			jsonObject.setResult(2);;  //发送失败		
		}
		return "success";
	}

	/**
	 * 邮箱验证
	 * @return
	 */
	public String checkEmail(){
		//先判断该邮箱是否注册过
		String email=userInfo.getEmail();
		if(userInfoBiz.checkEmail(email)>0){  //说明该邮箱已被注册
			jsonObject.setResult(0); //说明该邮箱已经被注册
		}else{
			jsonObject.setResult(1);  //说明邮箱可用
		}
		return "success";
	}
	
	/**
	 * 用户注册
	 * @return
	 */
	public String userRegister(){
		String regsafecode=userInfo.getFlag();
		userInfo.setFlag("");
		
		//判断用户输入的验证码是否跟系统生成的验证码相同
		Object obj=session.get(SessionAttributeNames.CHECKCODE);
		if(obj==null){
			jsonObject.setResult(1);  //说明校验码已经过期
		}else if(!regsafecode.equals(String.valueOf(obj))){
			jsonObject.setResult(2);  //说明校验码错误
		}else{
			//校验其他值是否正确，避免用户绕过前台校验
			if(userInfoBiz.addUserInfo(userInfo)>0){
				
				session.put(SessionAttributeNames.MARKINFO, "success"); //注册成功
				
				session.put(SessionAttributeNames.CHECKCODE, null);
				session.remove(SessionAttributeNames.CHECKCODE);
				jsonObject.setResult(3);   //说明注册成功
			}else{
				jsonObject.setResult(4);   //说明注册失败
			}
		}
		return "success";
	}
	
	/**
	 * 用户登录
	 * @return
	 */
	public String userLogin(){
		String code=userInfo.getFlag();
		String obj=(String) session.get("rand");
		if(!code.equals(obj)){
			jsonObject.setResult(1);;  //验证码错误
		}else{
			UserInfo uf=userInfoBiz.loginUserInfo(userInfo);
			if(uf!=null){
				
				session.put(SessionAttributeNames.CURRENTUSERINFO, uf);
				jsonObject.setResult(2);  //登录成功
			}else{
				jsonObject.setResult(3);  //登录失败
			}
		}
		return "success";
	}
	
	/**
	 * 获取登录用户信息
	 * @return
	 */
	public String getUserLogin(){
		Object obj=session.get(SessionAttributeNames.CURRENTUSERINFO);

		if(obj==null){  //说明没有登录
			jsonObject.setResult(1);;
		}else{
			UserInfo uf=(UserInfo)obj;
			jsonObject.setObj(uf);
		}
		return "success";
	}
	
	/**
	 * 用户退出登录
	 * @return
	 */
	public String userOutLogin(){
		session.put(SessionAttributeNames.CURRENTUSERINFO, null);
		session.remove(SessionAttributeNames.CURRENTUSERINFO);
		jsonObject.setResult(1);
		return "success";
	}
	
	/**
	 * 后台查询所有用户信息
	 * @return
	 */
	public String userInfoGet(){
		pageInfo.setMin(Integer.parseInt(request.getParameter("page")));
		pageInfo.setMax(Integer.parseInt(request.getParameter("rows")));
		jsonObject.setRows(userInfoBiz.findUserInfoByPage(pageInfo));
		jsonObject.setTotal(userInfoBiz.getTotal());
		return "success";
	}
	
	/**
	 * 删除用户信息
	 * @return
	 */
	public String userInfoDel(){
		jsonObject.setResult(userInfoBiz.delUserInfo(userInfo.getFlag()));
		return "success";
	}
	
	/**
	 * 后台用户更新
	 * @return
	 */
	public String userInfoUpdate(){
		UserInfo uf=(UserInfo) ReflectUtils.regInfo(request,UserInfo.class);
		jsonObject.setResult(userInfoBiz.update(uf));
		return "success";
	}
}
